package cn.ming.mingstarterexceltest.demos.web;

import cn.parker.excel.annotation.ExportExcel;
import cn.parker.excel.utils.ExcelUtil;
import lombok.SneakyThrows;
import org.apache.commons.compress.utils.Lists;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.IntStream;

/**
 * <pre>
 * 业务类
 * </pre>
 *
 * @author Parker
 * @version 1.0.0
 * @since 2025-07-29 22:32
 */
@RestController
@RequestMapping("/excel")
public class ExcelTestController {

    @SneakyThrows
    @GetMapping("/test")
    public void test(HttpServletResponse response) {
        ExcelUtil.exportExcel(getData(), "普通导出", ExportDemoVo.class, response);
    }
    @SneakyThrows
    @GetMapping("/test_1")
    public void test_1(HttpServletResponse response) {
        ExcelUtil.exportExcel(getData2(), "普通导出-多个同名列", ExportDemoVo2.class, response);
    }

    @SneakyThrows
    @GetMapping("/test2")
    @ExportExcel(fileName = "自动导出", modelClass = ExportDemoVo.class)
    public List<ExportDemoVo> test2() {
        return getData();
    }

    @SneakyThrows
    @GetMapping("/test2_1")
    @ExportExcel(fileName = "自动导出-模板", template = "template/export.xlsx")
    public List<ExportDemoVo> test2_1() {
        return getData();
    }

    @SneakyThrows
    @GetMapping("/test2_2")
    @ExportExcel(fileName = "自动导出-合并单元格", modelClass = ExportDemoVo3.class, cellMerge = true)
    public List<ExportDemoVo3> test2_2() {
        return getData3();
    }

    @SneakyThrows
    @GetMapping("/test2_3")
    @ExportExcel(fileName = "自动导出-压缩", modelClass = ExportDemoVo3.class, compress = true)
    public List<ExportDemoVo3> test2_3() {
        return getData3();
    }

    @SneakyThrows
    @GetMapping("/test2_4")
    @ExportExcel(fileName = "自动导出-分页", modelClass = ExportDemoVo3.class, sheetDataSize = 2)
    public List<ExportDemoVo3> test2_4() {
        return getData3();
    }

    private List<ExportDemoVo> getData() {
        List<ExportDemoVo> data = Lists.newArrayList();
        IntStream.range(0, 5).forEach(i -> {
            data.add(new ExportDemoVo("测试" + (i + 1), "0", "1", "18888888888", "test@test.com", "北京", "北京", "北京", LocalDateTime.now()));
        });
        return data;
    }

    private List<ExportDemoVo2> getData2() {
        List<ExportDemoVo2> data = Lists.newArrayList();
        IntStream.range(0, 5).forEach(i -> {
            data.add(new ExportDemoVo2("测试" + (i + 1), "0", "1", "18888888888", "test@test.com", "北京", "北京", "北京", LocalDateTime.now()));
        });
        data.add(new ExportDemoVo2("测试" + 3, "0", "1", "18888888888", "test@test.com", "北京", "北京", "北京", LocalDateTime.now()));
        return data;
    }
    private List<ExportDemoVo3> getData3() {
        List<ExportDemoVo3> data = Lists.newArrayList();
        IntStream.range(0, 5).forEach(i -> {
            data.add(new ExportDemoVo3("测试" + (i + 1), "0", "1", "18888888888", "test@test.com", "北京", "北京", "北京", LocalDateTime.now()));
        });
        data.add(new ExportDemoVo3("测试" + 3, "0", "1", "18888888888", "test@test.com", "河北", "石家庄", "市区", LocalDateTime.now()));
        data.add(new ExportDemoVo3("测试" + 3, "0", "1", "18888888888", "test@test.com", "北京", "北京", "北京", LocalDateTime.now()));
        return data;
    }
}
